package com.yilin.tms.system.controller;


import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.system.system.AppBanner;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.system.service.IAppBannerService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * APP广告控制器
 *
 * @author zhangjinyuan
 * @date 2022/8/3
 */
@RestController
@RequestMapping("/system/banner/")
public class AppBannerController extends BaseController {

    @Resource
    private IAppBannerService appBannerService;

    /**
     * 分页查询APP广告
     */
    @PostMapping(value = "getAppBannerPage")
    @UserTarget(UserType.adminUser)
    public ReturnData<Object> getAppBannerPage(AppBanner query, Integer page, Integer limit) {
        if (page == null) return jsonView(false, "查询页数为空");
        if (limit == null) return jsonView(false, "查询条数为空");
        PageData<AppBanner> pageData = appBannerService.getAppBannerPage(query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 获取上线中的广告集合
     */
    @PostMapping(value = "getReleaseAppBannerList")
    public ReturnData<Object> getReleaseAppBannerList() {
        List<AppBanner> appBanners = appBannerService.getReleaseAppBannerList();
        return jsonView(true, "获取成功", appBanners);
    }

    /**
     * 保存广告
     */
    @PostMapping(value = "saveAppBanner")
    @UserTarget(UserType.adminUser)
    public ReturnData<Object> saveAppBanner(AppBanner appBanner) {
        if (appBanner.getViewType() == null) {
            return jsonView(false, "广告类型为空");
        }
        if (StringUtil.isEmpty(appBanner.getTitle())) {
            return jsonView(false, "广告标题为空");
        }
        if (StringUtil.isEmpty(appBanner.getImageUrl())) {
            return jsonView(false, "图片地址为空");
        }
        if (StringUtil.isEmpty(appBanner.getFileUrl())) {
            return jsonView(false, "请先上传跳转文件");
        }
        appBannerService.saveAppBanner(getLoginUser(), appBanner);
        return jsonView(true, "保存成功");
    }

    /**
     * 修改广告
     */
    @PostMapping(value = "editAppBanner")
    @UserTarget(UserType.adminUser)
    public ReturnData<Object> editAppBanner(AppBanner appBanner) {
        if (StringUtil.isEmpty(appBanner.getId())) {
            return jsonView(false, "广告ID为空");
        }
        AppBanner localAppBanner = appBannerService.getAppBannerById(appBanner.getId());
        if (localAppBanner == null) {
            return jsonView(false, "未查询到该广告信息");
        }
        appBannerService.editAppBanner(getLoginUser(), appBanner, localAppBanner);
        return jsonView(true, "修改成功");
    }

    /**
     * 上线/下线广告
     */
    @PostMapping(value = "appBannerUpOrDown")
    @UserTarget(UserType.adminUser)
    public ReturnData<Object> appBannerUpOrDown(String appBannerId) {
        if (StringUtil.isEmpty(appBannerId)) {
            return jsonView(false, "广告Id为空");
        }
        AppBanner localAppBanner = appBannerService.getAppBannerById(appBannerId);
        if (localAppBanner == null) {
            return jsonView(false, "未查询到该广告");
        }
        boolean booln = appBannerService.appBannerUpOrDown(getLoginUser(), localAppBanner);
        return jsonView(true, booln ? "上线成功" : "下线成功");
    }

    /**
     * 删除广告
     */
    @PostMapping(value = "removeAppBanner")
    @UserTarget(UserType.adminUser)
    public ReturnData<Object> removeAppBanner(String[] appBannerIds) {
        if (appBannerIds == null || appBannerIds.length == 0) {
            return jsonView(false, "广告ID为空");
        }
        int successCount = appBannerService.removeAppBanner(getLoginUser(), appBannerIds);
        return jsonView(true, "删除成功：" + successCount + "条");
    }
}
